# -*- coding: utf-8 -*-
# Copyright 2011-2012 MagiCycles.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import threading

_local_locker = threading.local()

def init_cookies():
    """Init the cookies associated with the current request."""
    _local_locker._cookies = []


def add_cookie(cookie):
    """Sets the cookies associated with the current request."""
    _local_locker._cookies.append(cookie)


def clear_cookies():
    """Clear the cookies associated with the current request."""
    _local_locker._cookies = []


class MyCookiesMiddleware(object):
    def __init__(self):
        pass

    def process_request(self, request):
        clear_cookies()

    def process_response(self, request, response):
        for cookie in _local_locker._cookies:
            response.cookies.update(cookie)
        return response
